<?php
namespace app\index\Model;
use think\Model;

class TpCate extends Model
{
	public function getComCates(){
		$ComCateRes=$this->where(array('pid'=>0,'cate_type'=>5))->order('sort DESC')->select();
		foreach ($ComCateRes as $k => $v) {
			$ComCateRes[$k]['children']=$this->where(array('pid'=>$v['id']))->select();
		}
		return $ComCateRes;
	}


	//面包屑导航
	public function position($cateid){
		$data=$this->field('id,pid,cate_name')->select();
		return $this->_position($data,$cateid);
	}

	private function _position($data,$cateid){
		static $arr=array(); //为什么要用static
		$cates=$this->field('id,pid,cate_name')->find($cateid);
		if(empty($arr)){
         $arr[]=$cates;
      }

		foreach ($data as $k => $v) {
			if($v['id']==$cates['pid']){
				$arr[]=$v;
				$this->_position($data,$v['id']);
			}
		}
		return array_reverse($arr);
	}

   
}
